package com.maserb.monitor.web.personal;

import com.maserb.monitor.common.AjaxResult;
import com.maserb.monitor.common.AjaxUtils;
import com.maserb.monitor.common.Func_T;
import com.maserb.monitor.common.JpushImClientUtil;
import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.entity.schoolStudent.SchoolStudentParentEntity;
import com.maserb.monitor.repository.personal.SchoolAdviseRepository;
import com.maserb.monitor.service.author_manage.UserService;
import com.maserb.monitor.service.schoolStudent.SchoolStudentParentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

/**
 * 个人设置
 */
@Controller
@RequestMapping("/setting")
public class PersonalSettingController {

    @Autowired
    SchoolAdviseRepository adviseRepository;

    @Autowired
    SchoolStudentParentService parentService;

    @Autowired
    UserService userService;

    /**
     * @param oldPassword 老密码
     * @param newPassword 新密码
     * @param userId 家长id
     * @return
     */
    @RequestMapping("/modifyParentPassword")
    @ResponseBody
    public AjaxResult<Boolean> modifyParentPassword(String userId, String oldPassword, String newPassword) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {

                if(!StringUtils.isEmptyOrWhitespace(userId)){
                    SchoolStudentParentEntity parent = parentService.findOne(userId);
                    if(parent.getPassword().equals(oldPassword)) {
                        parent.setPassword(newPassword);
                        parentService.save(parent);
                    } else {
                        throw new Exception("密码错误!");
                    }
                }else{
                    throw new Exception("用户不正确!");
                }
                return true;
            }
        });
        /*AjaxResult<Boolean> result = new AjaxResult<>();
        result.setSuccess(true);

        SchoolStudentParentEntity parent = parentService.findOne(userId);
        if(!parent.getPassword().equals(oldPassword)) {
            result.setMessage("密码错误!");
            result.setData(false);
        } else {
            result.setSuccess(true);
            result.setData(true);
            parent.setPassword(newPassword);
            parentService.save(parent);
        }
        return result;*/
    }

    /**
     * @param oldPassword 老密码
     * @param newPassword 新密码
     * @param userId 老师id
     * @return
     */
    @RequestMapping("/modifyTeacherPassword")
    @ResponseBody
    public AjaxResult<Boolean> modifyTeacherPassword(String userId, String oldPassword, String newPassword) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {

                if(!StringUtils.isEmptyOrWhitespace(userId)){
                    UserEntity teacher = userService.findOne(userId);
                    if(teacher.getPassword().equals(oldPassword)) {
                        teacher.setPassword(newPassword);
                        userService.save(teacher);
                    } else {
                        throw new Exception("密码错误!");
                    }
                }else{
                    throw new Exception("用户不正确!");
                }
                return true;
            }
        });
        /*AjaxResult<Boolean> result = new AjaxResult<>();
        result.setSuccess(true);

        UserEntity teacher = userService.findOne(userId);
        if(!teacher.getPassword().equals(oldPassword)) {
            result.setMessage("密码错误!");
            result.setData(false);
        } else {
            result.setData(true);
            result.setSuccess(true);
            teacher.setPassword(newPassword);
            userService.save(teacher);
        }

        return result;*/
    }

    /**
     * 重新绑定家长手机
     * @param oldMobile 老手机号码
     * @param newMobile 新手机号码
     * @param userId 家长id
     * @return
     */
    @RequestMapping("/rebindingParentMobile")
    @ResponseBody
    public AjaxResult<Boolean> rebindingParentMobile(String userId, String oldMobile, String newMobile) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                if(!StringUtils.isEmptyOrWhitespace(userId)){
                    SchoolStudentParentEntity parent = parentService.findOne(userId);
                    if(parent.getPhoneNumber().equals(oldMobile)) {
                        SchoolStudentParentEntity parentEntity = parentService.findByUserName(newMobile);
                        if(null!=parentEntity && !StringUtils.isEmptyOrWhitespace(parentEntity.getUserName())){
                            throw new Exception("手机号码已被绑定!");
                        }else{
                            parent.setUserName(newMobile);
                            parent.setPhoneNumber(newMobile);
                            parentService.save(parent);
                            try{
                                JpushImClientUtil.addUser(parent.getUserName(),parent.getPassword(),parent.getRealName());
                            }
                            catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    } else {
                        throw new Exception("手机号码错误!");
                    }
                }else{
                    throw new Exception("用户不正确!");
                }
                return true;
            }
        });
        /*AjaxResult<Boolean> result = new AjaxResult<>();
        result.setSuccess(true);

        SchoolStudentParentEntity parent = parentService.findOne(userId);
        if(!parent.getUserName().equals(oldMobile)) {
            result.setMessage("手机号码错误!");
            result.setData(false);
        } else {
            result.setData(true);
            result.setSuccess(true);
            parent.setPhoneNumber(newMobile);
            parent.setUserName(newMobile);
            parentService.save(parent);
        }

        return result;*/
    }

    /**
     * 重新绑定老师手机
     * @param oldMobile 老手机号码
     * @param newMobile 新手机号码
     * @param userId 老师id
     * @return
     */
    @RequestMapping("/rebindingTeacherMobile")
    @ResponseBody
    public AjaxResult<Boolean> rebindingTeacherMobile(String userId, String oldMobile, String newMobile) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                if(!StringUtils.isEmptyOrWhitespace(userId)){
                    UserEntity teacher = userService.findOne(userId);
                    if(teacher.getPhoneNumber().equals(oldMobile)) {
                        teacher.setPhoneNumber(newMobile);
                        userService.save(teacher);
                    } else {
                        throw new Exception("手机号码错误!");
                    }
                }else{
                    throw new Exception("用户不正确!");
                }
                return true;
            }
        });
        /*AjaxResult<Boolean> result = new AjaxResult<>();
        result.setSuccess(true);

        UserEntity teacher = userService.findOne(userId);
        if(!teacher.getPhoneNumber().equals(oldMobile)) {
            result.setMessage("手机号码错误!");
            result.setData(false);
        } else {
            result.setData(true);
            result.setSuccess(true);
            teacher.setUserName(newMobile);
            teacher.setPhoneNumber(newMobile);
            userService.save(teacher);
        }

        return result;*/
    }
}
